package com.own.business.system.user.wechat.controller;

import com.own.aop.login.LoginIgnore;
import com.own.business.login.core.util.LoginCommonUtil;
import com.own.business.system.user.wechat.core.entity.form.WechatLoginForm;
import com.own.business.system.user.wechat.core.service.SystemUserWechatLoginService;
import com.own.component.common.login.util.LoginInfoUtil;
import com.own.component.common.login.util.SessionUserUtil;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.log.operation.aop.LogOperationAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * SystemUserWechatLoginController
 *
 * @author chenxueli
 * @date 2023-06-25 23:16:31
 */
@Slf4j
@Tag(name = "微信账号登录模块" + ConstantBeanSuffix.CONTROLLER)
@RestController
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + "system/user/wechat/login")
public class SystemUserWechatLoginController {

    @Resource
    private SystemUserWechatLoginService systemUserWechatLoginService;

    @Resource
    private LoginCommonUtil loginCommonUtil;
    @Resource
    private LoginInfoUtil loginInfoUtil;

    @LoginIgnore
    @LogOperationAop
    @Operation(summary = "微信openId登录")
    @PostMapping("/")
    public ResultModel<String> login(
            @RequestBody WechatLoginForm form,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        form.check();
        loginInfoUtil.type().set("wechat");
        loginInfoUtil.account().set(form.getOpenId());
        // 获取token
        var vo = loginCommonUtil.login(() -> systemUserWechatLoginService.login(form.getOpenId(), form.getAccountType()), request, response);
        // 返回信息
        return R.success(vo.getToken(), "登录成功");
    }

    @LogOperationAop
    @Operation(summary = "绑定微信号")
    @PostMapping("/bind")
    public ResultModel<Boolean> bind(
            @RequestBody WechatLoginForm form
    ) {
        form.check();
        var loginUser = SessionUserUtil.getLoginUser();
        systemUserWechatLoginService.bind(loginUser.userId(), form.getOpenId());
        return R.success(true, "绑定成功");
    }

    @LogOperationAop
    @Operation(summary = "解绑微信号")
    @PostMapping("/unbind")
    public ResultModel<Boolean> unbind() {
        var loginUser = SessionUserUtil.getLoginUser();
        systemUserWechatLoginService.unbind(loginUser.userId());
        return R.success(true, "解绑成功");
    }

    @LogOperationAop
    @Operation(summary = "是否已经绑定微信号")
    @GetMapping("/is/bind")
    public ResultModel<Boolean> isBind() {
        var loginUser = SessionUserUtil.getLoginUser();
        var flag = systemUserWechatLoginService.isBind(loginUser.userId());
        return R.success(flag, "查询成功");
    }

}
